﻿using Beetle;
using System;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
using System.Threading;

internal class Class_a : SocketAsyncEventArgs
{
    internal Class_ai _field_a;
    [CompilerGenerated]
    private Class_h _field_b;
    [CompilerGenerated]
    private TcpChannel _field_c;

    public Class_a()
    {
    }

    public Class_a(int A_0)
    {
        byte[] buffer = new byte[A_0];
        base.SetBuffer(buffer, 0, buffer.Length);
    }

    [CompilerGenerated]
    public Class_h a()
    {
        return this._field_b;
    }

    [CompilerGenerated]
    public void a(TcpChannel A_0)
    {
        this._field_c = A_0;
    }

    internal void a(Class_a A_0)
    {
        BufferWriter userToken = (BufferWriter) A_0.UserToken;
        try
        {
            TcpChannel channel = A_0.b();
            if (((A_0.SocketError == SocketError.Success) && (A_0.BytesTransferred > 0)) && (!channel._field_ae && channel._field_l._field_a))
            {
                if (TcpUtils.Statistics)
                {
                    Interlocked.Add(ref TcpUtils.SendBytes, (long) A_0.BytesTransferred);
                    Interlocked.Add(ref channel.SendBytes, (long) A_0.BytesTransferred);
                    Interlocked.Increment(ref TcpUtils.SendDataIO);
                }
                if (A_0.BytesTransferred < A_0.Count)
                {
                    int offset = A_0.Offset + A_0.BytesTransferred;
                    int count = A_0.Count - A_0.BytesTransferred;
                    A_0.SetBuffer(offset, count);
                    if (!channel.Socket.SendAsync(A_0))
                    {
                        this.a(A_0);
                    }
                }
                else
                {
                    Class_b _b = userToken.c();
                    A_0.a((TcpChannel) null);
                    A_0.UserToken = null;
                    _b = userToken.d();
                    if (_b != null)
                    {
                        _b.Dispose();
                    }
                    if (userToken._field_b.Count > 0)
                    {
                        if (TcpUtils.SendPacketSpacing > 0)
                        {
                            Thread.Sleep(TcpUtils.SendPacketSpacing);
                        }
                        channel.b(userToken);
                    }
                    else
                    {
                        if (TcpUtils.SendMessageSpacing > 0)
                        {
                            Thread.Sleep(TcpUtils.SendMessageSpacing);
                        }
                        if (channel.EnabledSendCompeletedEvent)
                        {
                            channel._field_ab.Success = true;
                            channel._field_ab.Messages = userToken._field_i;
                            channel.e();
                        }
                        channel.a(false);
                        channel.Send(null);
                    }
                }
            }
            else
            {
                if (channel.EnabledSendCompeletedEvent)
                {
                    channel._field_ab.Success = false;
                    channel._field_ab.Messages = userToken._field_i;
                    channel.e();
                }
                channel.CloseStatus = string.Format("Send:{0},{1},{2},{3}", new object[] { A_0.SocketError, A_0.BytesTransferred, channel._field_ad, channel._field_l._field_a });
                userToken.Dispose();
                channel.Dispose();
            }
        }
        catch (Exception exception)
        {
            try
            {
                ChannelErrorEventArgs e = new ChannelErrorEventArgs {
                    Channel = this.b(),
                    Exception = exception
                };
                this.b().CallChannelError(e);
            }
            catch
            {
            }
        }
    }

    [CompilerGenerated]
    public void a(Class_h A_0)
    {
        this._field_b = A_0;
    }

    [CompilerGenerated]
    internal TcpChannel b()
    {
        return this._field_c;
    }

    internal void b(Class_a A_0)
    {
        try
        {
            TcpChannel channel = A_0.b();
            if (((A_0.BytesTransferred > 0) && (A_0.SocketError == SocketError.Success)) && !channel._field_ae)
            {
                channel.TickCount = Environment.TickCount;
                if (TcpUtils.Statistics)
                {
                    Interlocked.Add(ref TcpUtils.ReceiveBytes, (long) A_0.BytesTransferred);
                    Interlocked.Add(ref channel.ReceiveBytes, (long) A_0.BytesTransferred);
                    Interlocked.Increment(ref TcpUtils.ReceiveDataIO);
                }
                if (A_0._field_a == null)
                {
                    A_0._field_a = new Class_ai();
                }
                A_0._field_a._field_f = channel;
                A_0._field_a._field_e = A_0;
                channel._field_o.a((Class_ad) A_0._field_a);
                Interlocked.Increment(ref channel._field_e);
                if ((TcpUtils.ChannelMaxQueueData > 0) && (channel._field_e > TcpUtils.ChannelMaxQueueData))
                {
                    channel.CloseStatus = "Channel Queue Data Overflow!";
                    channel.Dispose();
                }
                channel.BeginReceive();
            }
            else
            {
                channel.CloseStatus = string.Format("Receive:{0},{1},{2},{3}", new object[] { A_0.SocketError, A_0.BytesTransferred, channel._field_ad, channel._field_l._field_a });
                A_0.d();
                channel.Dispose();
            }
        }
        catch
        {
        }
    }

    public void c()
    {
        if (base.Buffer != null)
        {
            base.SetBuffer(0, base.Buffer.Length);
        }
    }

    public void d()
    {
        this.a((TcpChannel) null);
        if (this.a() != null)
        {
            this.a().a(this);
        }
    }

    protected override void OnCompleted(SocketAsyncEventArgs e)
    {
        base.OnCompleted(e);
        if (e.LastOperation == SocketAsyncOperation.Send)
        {
            this.a((Class_a) e);
        }
        else if (e.LastOperation == SocketAsyncOperation.Receive)
        {
            this.b((Class_a) e);
        }
        else if (e.SocketError != SocketError.Success)
        {
            this.b().CloseStatus = "completed SocketError:" + e.SocketError.ToString();
            this.b().Dispose();
        }
    }
}

